package com.example.a2032.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.Nullable;

import com.example.a2032.R;

public class ChargingBatteryView extends View {
    Paint mPaint;
    int lowBatteryColor = Color.parseColor("#ff2424");
    int normalBatteryColor = Color.parseColor("#00db0a");
    Bitmap bgBitmap;
    Bitmap lightingBitmap;
    Bitmap progressBitmap;
    float scaleHeightValue;
    float chargingProgress = 0.15f;

    public ChargingBatteryView(Context context) {
        super(context);
    }

    public ChargingBatteryView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private static final String TAG = "ChargingBatteryView";

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setColor(normalBatteryColor);
        mPaint.setAntiAlias(true);
        bgBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.bg_charging);
        lightingBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.charging_lighting);
        progressBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.charging_progress);
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        scaleHeightValue = 304f / bgBitmap.getHeight();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawProgress(canvas);
        drawLightning(canvas);
    }

    private void drawProgress(Canvas canvas) {
        mPaint.setColor(chargingProgress < 0.2f ? lowBatteryColor : normalBatteryColor);
        canvas.save();
        canvas.translate(0, 28);
        Matrix matrix = new Matrix();
        matrix.postScale(scaleHeightValue, scaleHeightValue);
        float progressBitmapHeight = progressBitmap.getHeight() * scaleHeightValue;
        RectF rectF = new RectF();
        rectF.left = 0;
        rectF.right = getWidth() - 2;
        rectF.bottom = progressBitmapHeight;
        rectF.top = rectF.bottom - progressBitmapHeight * chargingProgress;
        canvas.drawRect(rectF, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(progressBitmap, matrix, mPaint);
        mPaint.setXfermode(null);
        canvas.restore();
    }

    private void drawLightning(Canvas canvas) {
        canvas.save();
        canvas.translate(0, 28);
        Matrix matrix = new Matrix();
        matrix.postScale(scaleHeightValue, scaleHeightValue);
        canvas.drawBitmap(lightingBitmap, matrix, mPaint);
        canvas.restore();
    }

    private void drawBackground(Canvas canvas) {
        Matrix matrix = new Matrix();
        matrix.postScale(scaleHeightValue, scaleHeightValue);
        canvas.drawBitmap(bgBitmap, matrix, null);
    }

    public void setProgress(float chargingProgress) {
        this.chargingProgress = chargingProgress;
        postInvalidate();
    }

    ObjectAnimator animator;

    public void animProgress(float chargingProgress) {
        if (animator != null) animator.cancel();
        animator = ObjectAnimator.ofFloat(this, "progress",
                chargingProgress, 1);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(5000);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.setTarget(this);
        animatorSet.play(animator);
        animatorSet.start();

    }

    public void changeChargingProgress(int batteryValue) {
        float chargingProgress = batteryValue / 100f;
        animProgress(chargingProgress);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((int) (bgBitmap.getWidth() * scaleHeightValue), (int) (bgBitmap.getHeight() * scaleHeightValue));
    }
}
